#pragma once

#include <iostream>
#include <string>
#include "Log.hpp"
#include "InetAddr.hpp"

class Command
{
public:
    Command()
    {}
    ~Command()
    {}
    std::string Excute(const std::string &cmdstr)
    {
        FILE* fp = popen(cmdstr.c_str(), "r");
        if(fp)
        {
            char line[1024];
            while(fgets(line, sizeof(line), fp))
            {
                
            }
        }
        else
        {
            return "excute error";
        }
    }
    void HandlerCommand(int sockfd, InetAddr addr)
    {
        while(true)
        {
            char inbuffer[1024];
            LOG(INFO, "client: %s, message: %s\n", addr.AddrStr().c_str(), inbuffer);
            ssize_t n = ::read(sockfd, inbuffer, sizeof(inbuffer) - 1);
            if(n > 0)
            {
                inbuffer[n] = 0;
                std::string echo_string = "[server echo] #";

                echo_string += inbuffer;
                write(sockfd, echo_string.c_str(), echo_string.size());
            }
            else if(n == 0)
            {
                // 客户端退出
                LOG(INFO, "client %s quit\n", addr.AddrStr().c_str());
                break;
            }
            else
            {
                // 读出错
                LOG(ERROR, "read error : %s\n", addr.AddrStr().c_str());
                break;
            }
        }
    }
};

